<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('产品信息列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>[[#{productName}]]：</label>
                                <input type="text" name="productName"/>
                            </li>
                            <li>
                                <label>[[#{sku}]]：</label>
                                <input type="text" name="productCode"/>
                            </li>
                            <li>
                                <label>[[#{spu}]]：</label>
                                <input type="text" name="styleNo"/>
                            </li>
                            <li class="col-sm-3">
                                <label>[[#{product.category}]]：</label>
                                <div class="input-group">
                                    <input name="categoryId" class="form-control" type="hidden">
                                    <input id="suggest-category" class="form-control" type="text">
                                    <div class="input-group-btn">
                                        <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
                                            <span class="caret"></span>
                                        </button>
                                        <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                        </ul>
                                    </div>
                                </div>
                            </li>
                            <li>
                                <label>[[#{attributes}]]：</label>
                                <select name="attributes" th:with="type=${@dict.getType('jt_product_attributes')}">
                                    <option value="">[[#{all}]]</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>[[#{placeOfProduction}]]：</label>
                                <input type="text" name="countryOfOrigin"/>
                            </li>
                            <li class="col-sm-3">
                                <label>[[#{supplier}]]：</label>
                                <div class="input-group">
                                    <input name="supplierId" class="form-control" type="hidden">
                                    <input id="suggest-supplier" class="form-control" type="text">
                                    <div class="input-group-btn">
                                        <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
                                            <span class="caret"></span>
                                        </button>
                                        <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                        </ul>
                                    </div>
                                </div>
                            </li>
                            <li>
                                <label>[[#{status}]]：</label>
                                <select name="status" th:with="type=${@dict.getType('sys_normal_disable')}">
                                    <option value="">[[#{all}]]</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>[[#{createTime}]]：</label>
                                <input type="text" class="time-input" id="beginCreateTime" th:placeholder="#{beginTime}" name="params[beginCreateTime]"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endCreateTime" th:placeholder="#{endTime}" name="params[endCreateTime]" />
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;[[#{search}]]</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;[[#{reset}]]</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="business:product:add">
                    <i class="fa fa-plus"></i> [[#{add}]]
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="business:product:edit">
                    <i class="fa fa-edit"></i> [[#{edit}]]
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="business:product:remove">
                    <i class="fa fa-remove"></i> [[#{delete}]]
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="business:product:export">
                    <i class="fa fa-download"></i> [[#{export}]]
                </a>

                <a class="btn btn-info" onclick="importExcel()" shiro:hasPermission="business:product:edit">
                    <i class="fa fa-download"></i> [[#{import}]]
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-suggest-js" />
    <th:block th:include="include :: jasny-bootstrap-js" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('business:product:edit')}]];
        var removeFlag = [[${@permission.hasPermi('business:product:remove')}]];
        var attributesDatas = [[${@dict.getType('jt_product_attributes')}]];
        var statusDatas = [[${@dict.getType('sys_normal_disable')}]];
        var prefix = ctx + "business/product";

        $(function() {
            var options = {
                url: prefix + "/list",
                viewUrl: prefix + "/sn/{id}",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                importUrl: prefix + "/importData",
                importTemplateUrl: prefix + "/importTemplate",
                modalName: i18n('productInfo'),
                columns: [{
                    checkbox: true
                },
                {
                    field: 'productId',
                    title: '产品id',
                    visible: false
                },
                {
                    field: 'thumbnailUrl',
                    title: i18n('image'),
                    formatter: function(value, row, index) {
                        var imageUrl = value;
                        if(ctx.length > 1 && imageUrl != ""){
                            if(imageUrl.indexOf("/") == 0){
                                imageUrl = ctx + imageUrl.substr(1);
                            } else {
                                imageUrl = ctx + imageUrl;
                            }
                        }
                        return imageLazyLoadView(imageUrl, "auto", 750);
                    }
                },
                {
                    field: 'productName',
                    title: i18n('productName')
                },
                {
                    field: 'productCode',
                    title: i18n('sku'),
                    formatter: function (value, row, index) {
                        return value +'<a href="javascript:void(0)" onclick="$.operate.view(\'' + row.productId + '\')"><span class="badge badge-warning">序列号</span></a>';
                    }
                },
                {
                    field: 'styleNo',
                    title: i18n('spu')
                },
                {
                    field: 'categoryNameZh',
                    title: i18n('product.category')
                },
                {
                    field: 'attributes',
                    title: i18n('attributes'),
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(attributesDatas, value);
                    }
                },
                {
                    field: 'weight',
                    title: i18n('weight')+'(KG)'
                },
                {
                    field: 'unit',
                    title: i18n('unit')
                },
                {
                    field: 'cost',
                    title: i18n('costPrice')
                },
                {
                    field: 'purchasePrice',
                    title:  i18n('purchasePrice')
                },
                {
                    field: 'spec',
                    title: i18n('spec')
                },
                {
                    field: 'countryOfOrigin',
                    title: i18n('placeOfProduction')
                },
                {
                    field: 'supplierName',
                    title: i18n('supplier')
                },
                {
                    field: 'soldQty',
                    title: i18n('soldQty')
                },
                {
                    field: 'stock',
                    title: i18n('stock')
                },
                {
                    field: 'status',
                    title: i18n('status'),
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    field: 'createBy',
                    title: i18n('createBy')
                },
                {
                    field: 'createTime',
                    title: i18n('createTime'),
                    sortable:true
                },
                {
                    title: i18n('operate'),
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.productId + '\')"><i class="fa fa-edit"></i> '+i18n("edit")+'</a> ');
                        actions.push('<a class="btn btn-info btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="importSnExcel(\'' + row.productId + '\')"><i class="fa fa-edit"></i> '+i18n("import")+'</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.productId + '\')"><i class="fa fa-remove"></i>'+i18n("delete")+'</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });


        // 图片预览
        function imageLazyLoadView(value, height, width, target) {
            if ($.common.isEmpty(width)) {
                width = 'auto';
            }
            if ($.common.isEmpty(height)) {
                height = 'auto';
            }
            if($.common.isMobile()){
                target = "blank";
            }
            var windowWidth = $(window).width();
            //console.log("imgW====="+windowWidth)
            var imgW = 'img-md';
            if(windowWidth < 1024){
                imgW = 'img-lg';
            }
            // blank or self
            var _target = $.common.isEmpty(target) ? 'self' : target;
            if ($.common.isNotEmpty(value)) {
                return $.common.sprintf("<img class='img-circle "+imgW+" lazyload' data-type='2' data-height='%s' data-width='%s' data-target='%s' src='%s' />", height, width, _target, value);
            } else {
                return $.common.nullToStr(value);
            }
        }

        function importExcel() {
            table.options.modalName = "商品信息";
            table.options.importUrl = prefix + "/importData";

            $.table.importExcel();
        }

        function importSnExcel(id) {
            table.options.modalName = "商品序列号";
            table.options.importUrl = prefix + "/importSnData/"+id;

            $.table.importExcel('importSnTpl');
        }


        //商品序列号
        function importSnTemplate() {
            $.get(prefix + "/importSnTemplate", function(result) {
                if (result.code == web_status.SUCCESS) {
                    window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                } else if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                } else {
                    $.modal.alertError(result.msg);
                }
            });
        }
    </script>
    <script th:inline="javascript">
        var categorySuggest = $("#suggest-category").bsSuggest({
            url: ctx + "business/category/select/list",
            idField: "categoryId",
            keyField: "categoryNameZh",
            clearable: true,
            ignorecase: true,
            hideOnSelect: true,
            effectiveFields: ["categoryId", "categoryNameZh", "categoryNameEn", "categoryNameAr"],
            effectiveFieldsAlias: {categoryId: "分类ID", categoryNameZh: "中文", categoryNameEn: "English", categoryNameAr: "arabic"},
        }).on('onSetSelectValue', function (e, selectedData, selectedRawData) {
            console.log('onSetSelectValue: ', selectedData);

            $("input[name='categoryId']").val(selectedRawData.categoryId);
        }).on('onUnsetSelectValue', function (e) {
            //console.log("onUnsetSelectValue=="+e);
            $("input[name='categoryId']").val("");
        });


        var supplierSuggest = $("#suggest-supplier").bsSuggest({
            url: ctx + "business/supplier/select/list",
            idField: "supplierId",
            keyField: "supplierName",
            clearable: true,
            ignorecase: true,
            hideOnSelect: true,
            effectiveFields: ["supplierId", "supplierName","supplierCode"],
            effectiveFieldsAlias: {supplierId: "供应商ID", supplierName: "供应商名称", supplierCode:'供应商编码'},
        }).on('onSetSelectValue', function (e, selectedData, selectedRawData) {
            console.log('onSetSelectValue: ', selectedData);

            $("input[name='supplierId']").val(selectedRawData.supplierId);
        }).on('onUnsetSelectValue', function (e) {
            //console.log("onUnsetSelectValue=="+e);
            $("input[name='supplierId']").val("");
        });
    </script>

     <script th:inline="javascript">
         var ROOT = [[${#servletContext.contextPath}]];
         var LANG_COUNTRY = [[${#locale.language+'_'+#locale.country}]];

         //初始化i18n
         $.i18n.properties({
             path: ROOT + '/i18n/',//这里表示访问路径
             name: 'messages',//文件名开头
             language: LANG_COUNTRY,//文件名语言 例如en_US
             mode: 'map'//默认值
         });

         //i18n函数
         function i18n(msgKey) {
             try {
                 return $.i18n.prop(msgKey);
             } catch (e) {
                 return msgKey;
             }
         }
     </script>
</body>
<!-- 导入区域 -->
<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="file" name="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"/>
            <div class="mt10 pt5">
                导入商品信息
                <font style="color: #00c4ff;">
                    <input type="checkbox" id="isUpdateSupport" name="isUpdateSupport" title="如果已经存在，是否覆盖数据。"> 覆盖
                </font>
                <a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <font color="red" class="pull-left mt10">
                注意：1、点击了"导入"数据就会导入到系统，请勿重复操作。
                提示：仅允许导入“xls”或“xlsx”格式文件！
            </font>
        </div>
    </form>
</script>

<script id="importSnTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="file" name="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"/>
            <div class="mt10 pt5">
                导入产品序列号
                <a onclick="activeWindow().importSnTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <font color="red" class="pull-left mt10">
                注意：1、导入导入产品序列号，编码必填，对应栏目填写值。
                提示：仅允许导入“xls”或“xlsx”格式文件！
            </font>
        </div>
    </form>
</script>
</html>